package com.example.myimageshare.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextClock;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.myimageshare.Utils.DateUtil;
import com.example.myimageshare.Utils.MyTextUtil;
import com.example.myimageshare.View.PhotoViewActivity;
import com.example.myimageshare.R;
import com.example.myimageshare.domain.UserArt;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.ViewHolder> {

    private List<UserArt> users = new ArrayList<>();
    private AppCompatActivity context;
    private int resourceId;
    private int position;


    public HomeAdapter(List<UserArt> users){
        this.users = users;
    }

    public HomeAdapter(AppCompatActivity context, int resourceId, List<UserArt> users){
        this.context = context;
        this.resourceId = resourceId;
        this.users = users;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(
                resourceId,parent,false);
        final ViewHolder viewHolder = new ViewHolder(view);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
        final UserArt user = users.get(position);
        //不设置viewHoder的Tag，避免与Glide冲突
        this.position = position;

        if(user.getUsername() != null){
           holder.tv_name.setText(user.getUsername());
        }
        if(user.getEssay() != null){
            String str = MyTextUtil.Cut(user.getEssay());
            Log.d("HomeAdapter","strE:"+ str);
            holder.tv_artE.setText(str + "");
        }
        if(user.getUrls().size()!=0){
            Uri uri = Uri.parse(user.getUrls().get(0));
            Log.d("HomeAdapter","imgUrl: "+ uri);
            Glide.with(context).clear(holder.iv_art);
            Glide.with(context)
                    .load(uri)
                    .thumbnail(0.5f)
                    .into(holder.iv_art);
        }

        if(user.getCreatedAt()!=null){
            holder.tv_dateTime.setText(user.getCreatedAt());
        }

        holder.iv_art.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, PhotoViewActivity.class);
                Log.d("HomeAdapter",users.get(position).getUrls().toString());
                Bundle userArtBundle = new Bundle();
                userArtBundle.putSerializable("userArt", users.get(position));
                intent.putExtras(userArtBundle);
                intent.putExtra("currentPosition",1);

                context.startActivity(intent);

            }
        });
    }

    @Override
    public int getItemCount() {
        return users != null ? users.size() :0;
    }

    static class ViewHolder extends RecyclerView.ViewHolder{
        ImageView iv_art;
        TextView tv_name;
        ImageView iv_likes;
        TextView tv_dateTime;
        TextView tv_artE;


        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            iv_art = itemView.findViewById(R.id.iv_image_first);
            tv_name = itemView.findViewById(R.id.tv_username);
            iv_likes = itemView.findViewById(R.id.iv_likes);
            tv_dateTime = itemView.findViewById(R.id.tv_DateTime);
            tv_artE = itemView.findViewById(R.id.tv_artE);
        }


    }
}
